package com.archway.tools.path;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;


public class FolderMakerTool {

	private static String path;
	/**
	 * 
	 * <strong>(please edit it when context changed)</strong></br>
	 * <h1>描述:</h1>
	 * <li>请替换foldermaker.json 中  "path":"$C:\:path$" 的$C:\:path$ 为需要生成目录的路径 </li> 
	 * <li>请替换foldermaker.json 中  "$father$": { 的 $father$ 为需要生成的父模块</li>  
	 * <li>请替换foldermaker.json 中  "$child$": { 的 $child$ 为需要生成的子模块</li>  
	 * @param args
	 * @throws Exception
	 * @author lirn
	 * @createDate 2014年3月23日 上午12:57:25
	 */
	public static void main(String[] args) throws Exception {
		makeFolders();
	}
	
	static void makeFolders() throws Exception{
		File file = new File(FolderMakerTool.class.getResource("FolderMaker").toURI());
		Map<String, String> map = new HashMap<String, String>();
		ObjectMapper mapper = new ObjectMapper();
		map = mapper.readValue(file, Map.class);
		path = (String)map.get("path").replaceAll("#", ":");
		System.out.println("项目相对目录 :" + path);
		map.remove("path");
		reverseMapKeySet(map);
	}

	private static int count = 0;
	
	static void reverseMapKeySet(Object obj) {
		//count++;
		if (obj instanceof Map) {
			if (obj != null) {
				for (Object object : ((Map) obj).keySet()) {
					//System.out.println(coutSpace(count) + object);
					path = path+"/"+object;//递归前路径追加
					System.out.println(path);
					createFolder(path); //创建文件夹
					reverseMapKeySet(((Map) obj).get(object));//递归
					path = path.substring(0, path.lastIndexOf("/"+(String)object));//递归结束后路径减去
				}
			}
		} else if (obj instanceof String) {//非 null 直接有字符数据的情况，未来留给创建模版文件之用
		} else { //  null 的情况 ，未来留给创建模版文件之用
		}
		//count--;
	}

	static String coutSpace(int count) {
		String tmp = "";
		switch (count) {
		case 0: tmp = "--";break;
		case 1: tmp = "----";break;
		case 2: tmp = "------";break;
		case 3: tmp = "--------";break;
		case 4: tmp = "----------";break;
		case 5: tmp = "------------";break;
		case 6: tmp = "--------------";break;
		case 7: tmp = "----------------";break;
		case 8: tmp = "------------------";break;
		case 9: tmp = "--------------------";break;
		case 10:tmp = "----------------------";break;
		}
		return tmp;
	}
	
	private static  void createFolder(String path) {
		File file = new File(path);
		if(!file.exists()||!file.isDirectory()){
			file.mkdir();
		}
	}
}
